
#include "Bullet.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------------------- CLASS BULLET -------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////////////////

Bullet::Bullet()
{
    bullet_loc.x = 0;
    bullet_loc.y = 0;
    bullet_loc.h = 18;
    bullet_loc.w = 25;

    bullet_speed = 6;

    bullet_img.load( "grafx/bullets.png" );

    bullet_moving_right = false;

    bullet_id = '\0';
}

Bullet::Bullet( char bullet_id, int x, int y, bool facing_right )
{
    bullet_loc.x = x;
    bullet_loc.y = y;
    bullet_loc.h = 18;
    bullet_loc.w = 25;

    orgin_y = y;

    //bullet_slope = slope;

    if( true == facing_right )
        bullet_speed = 6;
    else
        bullet_speed = -6;

    bullet_img.load( "grafx/bullets.png" );

    bullet_moving_right = facing_right;

    this->bullet_id = bullet_id;
}

Bullet::~Bullet()
{
    //
}

void Bullet::Move()
{
    bullet_loc.x += bullet_speed;
}

void Bullet::Draw()
{
    bullet_img.drawAnim( bullet_loc.x, bullet_loc.y, bullet_loc.w, bullet_loc.h, 135, 40);
}

char Bullet::Get_bullet_id()
{
    return bullet_id;
}

void Bullet::Set_bullet_id( char bullet_id )
{
    this->bullet_id = bullet_id;
}

bool Bullet::Get_bullet_moving_right()
{
    return bullet_moving_right;
}

SDL_Rect Bullet::Get_bullet_loc()
{
    return bullet_loc;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------------------- CLASS BULLETS_CONTROL ----------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////////////////

Bullets_control::Bullets_control()
{

}

Bullets_control::~Bullets_control()
{
   Free_list_from_memory(); // free all memory allocated in the list
}

void Bullets_control::Check_bullets_off_screen()
{
    list< Bullet * >::iterator bullet_iter = bullets_list.begin();
    while( bullet_iter != bullets_list.end() )
    {
        // see if the bullet is off the screen
        // YOU WILL NOT BE ABLE TO CAUSE DAMAGE TO ENEMYS OUTSIDE OF THE SCREEN RANGE // STOP BITCHING //
        SDL_Rect bullet_sprite = (*bullet_iter)->Get_bullet_loc();
        if( bullet_sprite.x > GlobalScreen::screen->w
            || bullet_sprite.x < 0 )
        {
            //Bullet *tmp = (*bullet_iter);
            bullet_iter = Remove_bullet( bullet_iter );
           // delete tmp; // clean up that bullet
        }
        else
            bullet_iter++;
    }

}// Check_bullets_off_screen()

list< Bullet * >::iterator Bullets_control::Remove_bullet( list< Bullet * >::iterator iter )
{
    //cout << "remove bullet\n";
    list< Bullet * >::iterator return_tmp;
    Bullet *tmp = (*iter);
    return_tmp = bullets_list.erase( iter );
    //cout << "bullet removed from list, size: " << bullets_list.size() << endl;
    delete tmp; // clean up that bullet
    return return_tmp;
}

void Bullets_control::Add_bullet( Bullet *a_bullet )
{
    bullets_list.push_front( a_bullet );
    //cout << "bullet added to list, size: " << bullets_list.size() << endl;
}

void Bullets_control::Draw(Enemy_control &enemys, Player &player )
{
    Check_bullets_off_screen();

    list< Bullet * >::iterator bullet_iter = bullets_list.begin();

    while( bullet_iter != bullets_list.end() )
    {
        // move the bullet in the correct direction
        (*bullet_iter)->Move();
        (*bullet_iter)->Draw();
        bullet_iter++;
    }

}

void Bullets_control::Free_list_from_memory()
{
     list< Bullet * >::iterator bullet_iter = bullets_list.begin();

    for( bullet_iter; bullet_iter != bullets_list.end(); bullet_iter++ )
    {
        if( NULL != (*bullet_iter) )
        {
            Bullet *tmp = (*bullet_iter);
            delete tmp; // clean up that bullet
            //cout << "a bullet cleaned up in ~\n";
        }
    }

    bullets_list.clear();
}


list< Bullet * >::iterator Bullets_control::Get_bullets_control_iter()
{
    return bullets_class_iter;
}
list< Bullet * >::iterator Bullets_control::Get_bullets_control_end()
{
    bullets_class_iter = bullets_list.end();
    return bullets_class_iter;
}

list< Bullet * >::iterator Bullets_control::Get_bullets_control_begin()
{
    bullets_class_iter = bullets_list.begin();
    return bullets_class_iter;
}

